import allure
from appium.webdriver.common.appiumby import AppiumBy

from .base import Base


class Manage(Base):
    # 删除
    __DELETE1 = (AppiumBy.XPATH, '//*[@text="删除成员"]')
    __DELETE2 = (AppiumBy.XPATH, '//*[@text="删除"]')
    # 管理通讯录
    __ADDRESS_BOOK = (AppiumBy.XPATH, '//*[@text="管理通讯录"]')
    # 名字
    __NAME = (AppiumBy.XPATH, '//*[@text="姓名"]/../*[3]')
    # 账号
    __ACCOUNT = (AppiumBy.XPATH, '//*[@text="账号"]/../*[3]')
    # 别名
    __ALIAS = (AppiumBy.XPATH, '//*[@text="别名"]/../*[2]')
    # 性别
    __GENDER = (AppiumBy.XPATH, '//*[@text="性别"]/../*[2]')
    # 性别->男
    __GENDER_MAN = (AppiumBy.XPATH, '//*[@text="男"]')
    # 性别->女
    __GENDER_GRIL = (AppiumBy.XPATH, '//*[@text="女"]')
    # 座机
    __TELEPHONE = (AppiumBy.XPATH, '//*[@text="座机"]/../*[2]')
    # 邮箱
    __MAIN = (AppiumBy.XPATH, '//*[@text="邮箱"]/../*[2]')
    # 地址
    __ADDRESS = (AppiumBy.XPATH, '//*[@text="地址"]/../*[2]')
    # 地址->输入框
    __ADDRESS_INPUT = (AppiumBy.XPATH, '//*[@resource-id="android:id/content"]/*/*[2]/*')
    # 地址->确定
    __ADDRESS_CONFIRM = (AppiumBy.XPATH, '//*[@resource-id="android:id/content"]/*/*[1]/*[2]/*/*[@text="确定"]')
    # 职务
    __POST = (AppiumBy.XPATH, '//*[@text="职务"]/../*[2]')
    # 部门负责人->部门
    __DEPARTMENT_HEAD = (AppiumBy.XPATH, '//*[@text="负责部门"]/../*[2]/*/*/*/*/*/*/*/*[1]')
    # 部门负责人->按钮
    __DEPARTMENT_HEAD_BUTTON = (AppiumBy.XPATH, '//*[@text="部门负责人"]/../../../*[2]')
    # 对外职务
    __EXTERNAL_DUTIES = (AppiumBy.XPATH, '//*[@text="对外职务"]/../*[2]/*/*/*/*[1]')
    # 对外职务->设置
    __EXTERNAL_DUTIES_SET = (AppiumBy.XPATH, '//*[@text="设置专属对外职务"]')
    # 对外职务->输入框
    __EXTERNAL_DUTIES_INPUT = (AppiumBy.XPATH, '//*[@text="设置专属对外职务"]/../../*[4]/*[1]')
    # 对外职务->确定
    __EXTERNAL_DUTIES_CONFIRM = (AppiumBy.XPATH, '//*[@resource-id="android:id/content"]/*/*[1]/*[2]/*/*[@text="确定"]')
    # 保存
    __SAVE = (AppiumBy.XPATH, '//*[@resource-id="android:id/content"]/*/*[1]/*[2]/*/*[@text="保存"]')

    def check(self, name, img_name=None):
        with allure.step("查询成员"):
            __CHECK_NAME = (AppiumBy.XPATH, f'//android.widget.TextView[@resource-id="com.tencent.wework:id/gje" and @text="{name}"]')
            self.swip_by(__CHECK_NAME, operator="click", max_count=99)
            if img_name:
                self.wait(self.__NAME)
                self.img_log(img_name, "查询")
            return Manage(self.driver)

    def delete_member(self):
        with allure.step("删除成员"):
            self.swip_by(self.__DELETE1, operator="click")
            self.find_click(self.__DELETE2)
            text = self.find_text(self.__ADDRESS_BOOK)
            return text

    def get_check(self, get_message, img_name=None):
        with allure.step("查询成员信息"):
            text = ""
            # 名字
            if get_message == "name":
                text = self.swip_by(self.__NAME, operator="text")
            # 账号
            elif get_message == "account":
                text = self.swip_by(self.__ACCOUNT, operator="text")
            # 别名
            elif get_message == "alias":
                text = self.swip_by(self.__ALIAS, operator="text")
            # 性别
            elif get_message == "gender":
                text = self.swip_by(self.__GENDER, operator="text")
            # 座机
            elif get_message == "telephone":
                text = self.swip_by(self.__TELEPHONE, operator="text")
            # 邮箱
            elif get_message == "mail":
                text = self.swip_by(self.__MAIN, operator="text")
            # 地址
            elif get_message == "address":
                text = self.swip_by(self.__ADDRESS, operator="text")
            # 职务
            elif get_message == "post":
                text = self.swip_by(self.__POST, operator="text")
            # 部门负责人
            elif get_message == "department_head":
                text = self.swip_by(self.__DEPARTMENT_HEAD, operator="text")
            # 对外职务
            elif get_message == "external_duties":
                text = self.swip_by(self.__EXTERNAL_DUTIES, operator="text")
            if text:
                if img_name:
                    self.img_log(img_name, "查询成员信息")
                return text

    def correct(self, correct, img_name1, img_name2):
        with allure.step("修改成员信息"):
            # 名字
            if correct["name"]:
                self.swip_by(self.__NAME, operator="clear")
                self.find_key(self.__NAME, correct["name"])
            # 别名
            if correct["alias"]:
                self.swip_by(self.__ALIAS, operator="clear")
                self.find_key(self.__ALIAS, correct["alias"])
            # 性别
            if correct["gender"]:
                self.swip_by(self.__GENDER, operator="click")
                if correct["gender"] == "男":
                    self.find_click(self.__GENDER_MAN)
                elif correct["gender"] == "女":
                    self.find_click(self.__GENDER_GRIL)
            # 座机
            if correct["telephone"]:
                self.swip_by(self.__TELEPHONE, operator="clear")
                self.find_key(self.__TELEPHONE, correct["telephone"])
            # 邮箱
            if correct["mail"]:
                self.swip_by(self.__MAIN, operator="clear")
                self.find_key(self.__MAIN, correct["mail"])
            # 地址
            if correct["address"]:
                self.swip_by(self.__ADDRESS, operator="click")
                self.find_clear(self.__ADDRESS_INPUT)
                self.find_key(self.__ADDRESS_INPUT, correct["address"])
                self.find_click(self.__ADDRESS_CONFIRM)
            if img_name1:
                self.wait(self.__ADDRESS)
                self.img_log(img_name1, "截图保留1")
            # 职务
            if correct["post"]:
                self.swip_by(self.__POST, operator="clear")
                self.find_key(self.__POST, correct["post"])
            # 部门负责人
            if correct["department_head"]:
                self.swip_by(self.__DEPARTMENT_HEAD_BUTTON, operator="click")
            # 对外职务
            if correct["external_duties"]:
                self.swip_by(self.__EXTERNAL_DUTIES, operator="click")
                self.find_click(self.__EXTERNAL_DUTIES_SET)
                self.find_clear(self.__EXTERNAL_DUTIES_INPUT)
                self.find_key(self.__EXTERNAL_DUTIES_INPUT, key=correct['external_duties'])
                self.find_click(self.__EXTERNAL_DUTIES_CONFIRM)
            if img_name2:
                self.wait(self.__EXTERNAL_DUTIES)
                self.img_log(img_name2, "截图保留2")
            # 保存
            self.find_click(self.__SAVE)
            # 返回 管理通讯录
            text = self.find_text(self.__ADDRESS_BOOK)
            return text
